<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;

/**
 * 文章 125
 */
class Articlestest extends Api
{
    protected $noNeedLogin = ['login', 'article_type', 'article_lists', 'article_detail', 'article_click', 'article_comment','article_comment_click'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->auth->nickname]);
    }

    /**
     * 文章类型
     *
     * @ApiMethod (POST)
     * @param string $pid  文章分类一级id
     */
    public function article_type()
    {
        $Articles= new \app\common\model\Articles;
        $Category= new \app\common\model\Category;
        $pid = $this->request->post('pid',14);
        $data=$Category->where("pid=".$pid)->select();
        $this->success("获取成功", $data);
    }

    /**
     * 文章列表
     *
     * @ApiMethod (POST)
     * @param string $category_id  文章类别id
     */
    public function article_lists()
    {
        $Articles= new \app\common\model\Articles;
        $Category= new \app\common\model\Category;
        $category_id = $this->request->post('category_id');
        if(empty($category_id)){
            $this->error("文章类别id必传");
        }
        $data=$Articles->where("category_id=".$category_id." and delete_time is null")
            ->alias("a")
            ->join("fa_category c", "c.id = a.category_id", "LEFT")
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->select();
        $this->success("获取成功", $data);
    }

    /**
     * 文章详情
     *
     * @ApiMethod (POST)
     * @param string $id  文章id
     */
    public function article_detail()
    {
        $Articles= new \app\common\model\Articles;
        $Category= new \app\common\model\Category;
        $id = $this->request->post('id');
        if(empty($id)){
            $this->error("文章id必传");
        }
        $data=$Articles->where("a.id=".$id." and delete_time is null")
            ->alias("a")
            ->join("fa_category c", "c.id = a.category_id", "LEFT")
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->find();
        if(!empty($data)){
            $save_data["read_num"]=$data["read_num"]+1;
            $Articles->where("id=".$id)
                ->update($save_data);
        }

        $this->success("获取成功", $data);
    }
    /**
     * 文章点赞
     *
     * @ApiMethod (POST)
     * @param string $id  文章id
     */
    public function article_click()
    {
        $Articles= new \app\common\model\Articles;
        $Category= new \app\common\model\Category;
        $id = $this->request->post('id');
        if(empty($id)){
            $this->error("文章id必传");
        }
        $data=$Articles->where("a.id=".$id." and delete_time is null")
            ->alias("a")
            ->join("fa_category c", "c.id = a.category_id", "LEFT")
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->find();
        if(!empty($data)){
            $save_data["click"]=$data["click"]+1;
            $Articles->where("id=".$id)
                ->update($save_data);
            $this->success("成功");
        }else{
            $this->error("该文章不存在或已删除");
        }

    }


    /**
     * 文章评论
     *
     * @ApiMethod (POST)
     * @param string $id  文章id
     */
    public function article_comment()
    {
        $Articles= new \app\common\model\Articles;
        $ArticlesComment= new \app\common\model\ArticlesComment;
        $Category= new \app\common\model\Category;
        $uid = $this->auth->id;
        $id = $this->request->post('id');
        $com_content = $this->request->post('com_content');
        $type = $this->request->post('type',0);//0 一级评论  1 二级评论
        if(empty($id)){
            $this->error("文章id必传");
        }
        if(empty($com_content)){
            $this->error("评论内容必传");
        }
        $data=$Articles->where("a.id=".$id." and delete_time is null")
            ->alias("a")
            ->join("fa_category c", "c.id = a.category_id", "LEFT")
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->find();
        //var_dump($uid);die;
        if(!empty($data)){
            if($type==0){
                $save_data["article_id"]=$id;
                $save_data["addtime"]=time();
                $save_data["uid"]=$uid;
                $save_data["com_content"]=$com_content;
                $ArticlesComment
                    ->insert($save_data);
                $this->success("成功");
            }elseif($type==1){
                $com_id= $this->request->post('com_id',0);//针对哪条评论的评论id
                if(empty($com_id)){
                    $this->error("二级评论，上级评论id必传");
                }
                $save_data["article_id"]=$id;
                $save_data["addtime"]=time();
                $save_data["uid"]=$uid;
                $save_data["com_pid"]=$com_id;
                $save_data["com_content"]=$com_content;
                $ArticlesComment
                    ->insert($save_data);
                $this->success("成功");
            }

        }else{
            $this->error("该文章不存在或已删除");
        }

    }



    /**
     * 文章评论列表
     *
     * @ApiMethod (POST)
     * @param string $id  文章id
     */
    public function article_comment_list555()
    {
        $Articles= new \app\common\model\Articles;
        $ArticlesComment= new \app\common\model\ArticlesComment;
        $Category= new \app\common\model\Category;
        $id = $this->request->post('id');
        $uid = $this->auth->id;
        if(empty($id)){
            $this->error("文章id必传");
        }

        $data=$Articles
            ->alias("a")
            ->join("fa_category c", "c.id = a.category_id", "LEFT")
            ->where("a.id=".$id." and delete_time is null")
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->find();
        if(!empty($data)){
            $data_comment=$ArticlesComment
                ->alias("c")
                ->join("fa_user u", "u.id = c.uid", "LEFT")
                ->where("c.article_id=".$id." and c.com_pid=0 and delete_time is null")
                ->field("c.*, u.avatar")
                ->order("c.id desc")
                //->buildSql();
                ->select();
            //var_dump($data_comment);die;
            foreach($data_comment as $key=>$value){
                $data_comment[$key]["com_sons"]=$ArticlesComment
                    ->alias("c")
                    ->join("fa_user u", "u.id = c.uid", "LEFT")
                    ->where("c.article_id=".$id." and c.com_pid=".$value["id"])
                    ->field("c.*, u.avatar")
                    ->order("c.id desc")
                    ->select();
            }
            $this->success("获取成功", $data_comment);
        }else{
            $this->error("该文章不存在或已删除");
        }


    }

    public function article_comment_list()
    {
        $Articles= new \app\common\model\Articles;
        $ArticlesComment= new \app\common\model\ArticlesComment;
        $Category= new \app\common\model\Category;
        $UClick= new \app\common\model\UClick;
        $id = $this->request->post('id');
        $uid = $this->auth->id;



        $page =$this->request->request("page", 0);
        $limit = $this->request->request("limit", 10);
        if(empty($limit)) $limit = 10;
        if($limit > 20) $limit = 20;
        $offset = $page * $limit;


        if(empty($uid)){
            $this->error("token必传");
        }
        if(empty($id)){
            $this->error("文章id必传");
        }

        $data=$Articles
            ->alias("a")
            ->join("fa_category c", "c.id = a.category_id", "LEFT")
            ->where("a.id=".$id." and delete_time is null")
            ->field("a.*, c.name type_name")
            ->order("id desc")
            ->find();
        if(!empty($data)){
            $data_comment=$ArticlesComment
                ->alias("c")
                ->join("fa_user u", "u.id = c.uid", "LEFT")
                ->where("c.article_id=".$id." and c.com_pid=0 and delete_time is null")
                ->field("c.*, u.avatar,u.nickname,u.username")
                ->order("c.id desc")
                //->buildSql();
                ->limit("$offset, $limit")
                ->select();
            $result_all = $ArticlesComment//该文章所有的评论
            ->alias("c")
                ->join("fa_user u", "u.id = c.uid", "LEFT")
                ->where("c.article_id=".$id." and delete_time is null")
                ->field("c.*, u.avatar,u.nickname,u.username")
                ->order("c.id desc")
                //->buildSql();
                ->select();
            //var_dump($data_comment);die;
            if(!empty($data_comment)){
                foreach($data_comment as $key=>$value){
                    //var_dump($uid);var_dump($value["id"]);die;
                    $first_click[$key]=$UClick->where("uid=".$uid." and type =1 and comment_id=".$value["id"])
                        ->find();
                    if(!empty($first_click[$key])){
                        $data_comment[$key]["is_click"]=1;
                    }else{
                        $data_comment[$key]["is_click"]=0;
                    }
                    $data_comment[$key]["com_sons"]=$this->getChilds($result_all,$value["id"],$uid);
                }
            }

            $this->success("获取成功1", $data_comment);
        }else{
            $this->error("该文章不存在或已删除");
        }


    }


    //递归获取层级数组  结构化数组
    public function getChilds($lists,$pid,$uid){
        $UClick= new \app\common\model\UClick;
        $list = [];
        $i=0;
        foreach ($lists as $value) {
            if ($value['com_pid'] == $pid) {
                $first_click=$UClick->where("uid=".$uid." and type =1 and comment_id=".$value["id"])
                    ->find();
                if(!empty($first_click)){
                    $value->is_click=1;
                }else{
                    $value->is_click=0;
                }
                //var_dump($value->toArray());die;
                $list[$i]=$value;
                $list[$i]['com_sons']=self::getChilds($lists,$value['id'],$uid);
            }
            $i++;
        }
        if($list){
            $list = array_values($list);
            return $list;
        }else{
            return [];
        }

    }

    /**
     * 文章 评论点赞
     *
     * @ApiMethod (POST)
     * @param string $id  评论id
     */
    public function article_comment_click()
    {
        $Articles= new \app\common\model\Articles;
        $Category= new \app\common\model\Category;
        $ArticlesComment= new \app\common\model\ArticlesComment;
        $com_id = $this->request->post('com_id');
        if(empty($com_id)){
            $this->error("评论id必传");
        }
        $data=$ArticlesComment->where("id=".$com_id)
            ->order("id desc")
            ->find();
        if(!empty($data)){
            $save_data["click_num"]=$data["click_num"]+1;
            $ArticlesComment->where("id=".$com_id)
                ->update($save_data);
            $this->success("成功");
        }else{
            $this->error("该评论不存在或已删除");
        }

    }


}
